package com.hotfire.template.common;

/**
 * 分页组件
 * 
 * 示例：item代码数据库获取的数据,总共分为4页，第4页有3条数据
 * item1
 * item2
 * item3
 * <上一页><第1页(高亮显示)><第2页><第3页><第4页><下一页>
 * 
 * 此时：
 * showItemCount=3, totalItemCount=12(3*4),totalPageCount=4,currentPage=1
 * 
 * @author kuangzhongwei
 *
 */
public class Paginator {
	
	// 每页显示多少条数据,默认10条
	private Integer showItemCount = 12;
	
	// 总共多少条数据
	private Integer totalItemCount = -1;
	
	// 总共多少页
	private Integer totalPageCount = 0;
	
	// 当前页数
	private Integer currentPage = 1;

	/**
     * @return the showItemCount
     */
    public Integer getShowItemCount() {
        return showItemCount;
    }

    /**
     * @param showItemCount the showItemCount to set
     */
    public void setShowItemCount(Integer showItemCount) {
        this.showItemCount = showItemCount;
    }

    /**
     * @return the totalItemCount
     */
    public Integer getTotalItemCount() {
        return totalItemCount;
    }

    /**
     * @param totalItemCount the totalItemCount to set
     */
    public void setTotalItemCount(Integer totalItemCount) {
        this.totalItemCount = totalItemCount;
    }

    /**
     * @param totalPageCount the totalPageCount to set
     */
    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	

    public Integer getTotalPageCount() {
        int count =  this.totalItemCount / this.showItemCount;
        if(this.totalItemCount % this.showItemCount != 0){
            count += 1;
        }
        this.totalPageCount = count;
        return this.totalPageCount;
    }

	/**
	 * 查询开始index
	 * @return
	 */
    public Integer getStart() {
        return (currentPage-1) * this.showItemCount;
    }
    
	public int getOffset() {
	    return this.showItemCount;
	}

	/**
	 * 默认的，无分页的分页器。即limit 0, 9999999999(Integer.max)
	 * @return
	 */
    public static Paginator generateDefaltInstance() {
        Paginator p = new Paginator();
        p.setCurrentPage(1);
        p.setShowItemCount(Integer.MAX_VALUE);
        return p;
    }
}
